import { observable, action, toJS } from 'mobx';
import { Bubble, Tools } from 'weaver-mobile';
import { ListPage, RouteLayout, ListViewPage } from 'weaver-mobile-page';
import * as Api from '../apis';

const getLabel = Tools.getLabel;
const initState = {
  voteId: '',
  dataInfo: {},
  voteLoading: true,
  groupid: '',
  isgroup: '',
  viewRight: false,
  personSearchName: '',
  personSearch: false,
  imgArr: [],
  saveLoading: false,
}
export default class VoteStore {
  path;
  personListLayout;
  usertype = '';
  optionid = '';
  @observable personList;
  @observable voteOptionList = new ListPage.Store({ route: '/groupVote/voting' });
  @observable voteId = initState.voteId;
  @observable dataInfo = initState.dataInfo;
  @observable voteLoading = initState.voteLoading;
  @observable groupid = initState.groupid;
  @observable isgroup = initState.isgroup;
  @observable viewRight = initState.viewRight; // 查看权限
  @observable personSearchName = initState.personSearchName; // 人员列表搜索姓名
  @observable personSearch = initState.personSearch;
  @observable imgArr = initState.imgArr;
  @observable saveLoading = initState.saveLoading;
  
  initLayout = () => {
    this.personListLayout = new RouteLayout.Store({ route: this.path, id: 'pesonlist'});
    this.personList = new ListViewPage.Store({ route: this.path, id: 'list' });
  }

  @action setStoreState = (params, callback) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    if (callback) callback();
  }

  @action clear = () => this.setStoreState(initState);

  /* 取消选择 */
  @action clearSelect = () => this.voteOptionList.selectNone();

  @action viewGroupVote = (params, refresh = false) => {
    if (!refresh) {
      this.voteLoading = true;
    }
    params = { 
      votingid: this.voteId, 
      ...params
    }
    if (this.groupid) {
      params = {...params, groupid: this.groupid}
    }
    Api.viewGroupVote(params).then(res => {
      this.viewRight = res.isExist === '1';
      if(res.isExist === '1') {
        this.dataInfo = res.votedata;
        this.voteOptionList.setState({data: res.votedata.options});

        // 图片处理
        if (res.votedata && res.votedata.themepicsrc) {
          this.imgArr.push({ url: res.votedata.themepicsrc })
        }
        if (res.votedata.options) {
          res.votedata.options.forEach(item => {
            if (item.optionimg !== '') {
              item.optionimg.split(',').map(img => {
                this.imgArr.push({ url: img })
                return ''
              })
            }
          })
        }
      }
      this.voteLoading = false;
    })
  }

  /* 投票 */
  @action onVote = (params, callback) => {
    let select = toJS(this.voteOptionList.selectedRowKeys);
    let data = toJS(this.dataInfo);
    if (select.length === 0) {
      Bubble.error(getLabel('516188','请选择！'));
      return false;
    }

    if (data.maxvoteoption !== '0') {
      if (data.choosemodel === '1' && (Number(data.maxvoteoption) < select.length)) {
        Bubble.error(getLabel('516189','您已超出最大可选项数！'));
        return false;
      }
    }

    this.saveLoading = true
    params = {
      votingid: this.voteId,
      groupid: this.groupid,
      votooptioncheck: select.join(),
      ...params,
    }
  
    Api.savegroupvoteresult(params).then(res => {
      if (res.status === '1') {
        Bubble.success(getLabel('516190','投票成功！'));
        this.viewGroupVote();
        this.saveLoading = false
        if (callback && typeof callback === 'function') callback(res);
      }
    })
  }

  /************************************   投票人员--列表   ************************************/ 
  @action groupchatvoteprelist = (params) => {
    params = {
      votingid: this.voteId,
      groupid: this.groupid, 
      usertype: this.usertype,
      ...params
    }
    if (this.optionid) {
      params = {...params, optionid: this.optionid}
    }
    if (this.personSearchName && this.personSearch) {
      params = {...params, lastname: this.personSearchName}
    }
    Api.groupchatvoteprelist(params).then(res => {
      const dataKey = res.sessionkey;
      this.personList.getData({ dataKey }, () => this.voteListLoading = false, true)
    })
  }
}